package com.hunlian.makelove.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hunlian.makelove.R;
import com.hunlian.makelove.net.NetTools;

/**
 * 访问网页
 * 
 * @author Buddy 2015-01-13
 */
public class WebViewActivity extends BaseActivity {
	private WebView mWebView;
	private String mUrlAddress = "";
	private ImageButton mIbBack;
	private RelativeLayout title_bar_rl;
	private TextView mTvTitle;
	private String sTitle;
	private String actNo;
	private RelativeLayout netFailed;
	private Button btnReLoad;
	private WebViewActivity mSelf;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_webview);
		mSelf = this;
		mUrlAddress = getIntent().getStringExtra("url");
		//mUrlAddress = "http://h5.m.taobao.com/ww/index.htm?spm=0.0.0.0&ttid=sys@mytb_h5_1.0.0#!dialog-Y250YW9iYW/kuK3lm73nlLXkv6Hlrpjmlrnml5foiLDlupc65a6iNTg=-14459078753--1075734165733413";
		sTitle = getIntent().getStringExtra("title");
		actNo = getIntent().getStringExtra("actNo");
		

		findID();
		init();

		if (NetTools.isConnect(WebViewActivity.this) == false) {
			netFailed.setVisibility(View.VISIBLE);
		}

		WebSettings webSettings = mWebView.getSettings();
		// 设置WebView属性，能够执行Javascript脚本
		webSettings.setJavaScriptEnabled(true);
		// 设置可以访问文件
		webSettings.setAllowFileAccess(true);
		webSettings.setDefaultTextEncodingName("utf-8");
		// 设置支持缩放
		webSettings.setBuiltInZoomControls(true);
		webSettings.setUseWideViewPort(true); 
		webSettings.setLoadWithOverviewMode(true); 
		//加载过程中出现Uncaught TypeError: Cannot call method 'getItem' of null at这样的错误加上这句
		//参考自http://blog.csdn.net/yuri99/article/details/46378137
		webSettings.setDomStorageEnabled(true);

		// 加载需要显示的网页
		mWebView.loadUrl(mUrlAddress);
		// 设置Web视图
		mWebView.setWebViewClient(new WebViewClientEx());
		
	}

	private void findID() {
		title_bar_rl = (RelativeLayout) this.findViewById(R.id.ll_title);
		mTvTitle = (TextView) title_bar_rl.findViewById(R.id.title);
		mIbBack = (ImageButton) title_bar_rl.findViewById(R.id.back);
		mWebView = (WebView) this.findViewById(R.id.webView);
		netFailed = (RelativeLayout) this.findViewById(R.id.netFailed);
		btnReLoad = (Button) netFailed.findViewById(R.id.btnReLoad);
		final ProgressBar myProgressBar = (ProgressBar) this.findViewById(R.id.myProgressBar);
		
		mWebView.setWebChromeClient(new WebChromeClient(){
			
			  @Override
	          public void onProgressChanged(WebView view, int newProgress) {
	              if (newProgress == 100) {
	            	  myProgressBar.setVisibility(View.INVISIBLE);
	              } else {
	                  if (View.INVISIBLE == myProgressBar.getVisibility()) {
	                	  myProgressBar.setVisibility(View.VISIBLE);
	                  }
	                  myProgressBar.setProgress(newProgress);
	              }
	              super.onProgressChanged(view, newProgress);
	          }

		});
		
		mIbBack.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				WebViewActivity.this.finish();
			}
		});

		btnReLoad.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (NetTools.isConnect(WebViewActivity.this) == false) {
					netFailed.setVisibility(View.VISIBLE);
				} else {
					netFailed.setVisibility(View.GONE);
					mWebView.reload();
				}
			}
		});
	}

	private void init() {
		mTvTitle.setText(sTitle);
	}

	@Override
	// 设置回退
	// 覆盖Activity类的onKeyDown(int keyCoder,KeyEvent event)方法
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (mWebView.canGoBack()) {
				mWebView.goBack(); // goBack()表示返回WebView的上一页面
				return true;
			}
			// 如果已经回到了无法回退的页面，则关闭此页
			return super.onKeyDown(keyCode, event);
		}
		return false;

	}

	// Web视图
	private class WebViewClientEx extends WebViewClient {
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			//view.loadUrl(url);
			return super.shouldOverrideUrlLoading(view, url);
		}
		
		@Override
		public void onReceivedError(WebView view, int errorCode,
				String description, String failingUrl) {
			//Printout.println("webview加载失败，路径为："+failingUrl);
			super.onReceivedError(view, errorCode, description, failingUrl);
		}
		
		@Override
		public void onPageStarted(WebView view, String url, Bitmap favicon) {
			super.onPageStarted(view, url, favicon);
		}
	}
	
	private class MyWebChromeClient extends WebChromeClient{
		@Override
        public void onProgressChanged(WebView view, int newProgress) {
			final ProgressBar myProgressBar = (ProgressBar) WebViewActivity.this.findViewById(R.id.myProgressBar);
            if (newProgress == 100) {
          	  myProgressBar.setVisibility(View.INVISIBLE);
            } else {
                if (View.INVISIBLE == myProgressBar.getVisibility()) {
              	  myProgressBar.setVisibility(View.VISIBLE);
                }
                myProgressBar.setProgress(newProgress);
            }
            super.onProgressChanged(view, newProgress);
        }
	}

	public static void CallWebViewActivity(Context context, String url) {
		Intent intent = new Intent();
		intent.setClass(context, WebViewActivity.class);
		intent.putExtra("url", url);
		context.startActivity(intent);
	}

	public static void CallWebViewActivity(Context context, String title,
			String url) {
		Intent intent = new Intent();
		intent.setClass(context, WebViewActivity.class);
		intent.putExtra("title", title);
		intent.putExtra("url", url);
		context.startActivity(intent);
	}
	
	


}
